<?php
if (false)$member = new ORM_Admin_Member_Data();
?>
<script type="text/javascript">
var _is_run_ajax = false;
var now_groups_form = '';
function do_next_step()
{
    if (_is_run_ajax)return;
    var checkboxs = MyQEE.$('checked_perm_div').getElementsByTagName('input');
    var group_ids = [];
    for ( var i=0;i<checkboxs.length;i++ )
    {
        if ( checkboxs[i].checked )
        {
            group_ids.push(checkboxs[i].value);
        }
    }
    if (!group_ids.length)
    {
        MyQEE.alert('请先选择一个权限组');
        return ;
    }
    var groups_form = group_ids.join(',');
    if ( now_groups_form==groups_form )
    {
        //直接切换显示
        MyQEE.$('mytag_main_1').style.display='none';
        MyQEE.$('mytag_main_2').style.display='';
        MyQEE.$('step_tag_1').className='';
        MyQEE.$('step_tag_2').className='hover';
        return ;
    }

    _is_run_ajax = true;
    var ajax = new MyQEE.ajax('<?php echo Core::url('administrator/edit_perm_form/'.$member->id);?>');
    ajax.method = 'GET';
    ajax.setVar('group_ids',group_ids);
    ajax.onError = function()
    {
        _is_run_ajax = false;
        MyQEE.$('step1_loading').style.display='none';
        MyQEE.Msg('数据请求失败，请重试。');
    }

    ajax.onCompletion = function()
    {
        _is_run_ajax = false;
        MyQEE.$('step1_loading').style.display='none';
        MyQEE.$('mytag_main_1').style.display='none';
        MyQEE.$('mytag_main_2').style.display='';
        MyQEE.$('step_tag_1').className='';
        MyQEE.$('step_tag_2').className='hover';
        MyQEE.$('perm_div').innerHTML = this.response;

        change_super_perm(MyQEE.$('perm_setting_super_admin'));

        now_groups_form = groups_form;
    }

    ajax.runAJAX();
    MyQEE.$('step1_loading').style.display='';
}
function do_per_step()
{
    MyQEE.$('mytag_main_1').style.display='';
    MyQEE.$('mytag_main_2').style.display='none';
    MyQEE.$('step_tag_1').className='hover';
    MyQEE.$('step_tag_2').className='';
}
function change_super_perm(obj)
{
    var objs = MyQEE.$('perm_checkbox_div').getElementsByTagName('input');
    for(var i=0;i<objs.length;i++)
    {
        if (obj.checked)
        {
            objs[i].checked = 'checked';
            objs[i].disabled = 'disabled';
        }
        else
        {
            if ( objs[i].getAttribute('auto_disabled')=='on' )
            {
                objs[i].disabled = 'disabled';
            }
            else
            {
                objs[i].disabled = false;
            }
        }
    }
}

function change_group()
{
    var checkboxs = MyQEE.$('checked_perm_div').getElementsByTagName('input');
    var group_ids = [];
    for ( var i=0;i<checkboxs.length;i++ )
    {
        if ( checkboxs[i].checked )
        {
            group_ids.push(checkboxs[i].value);
        }
    }
    var tmphtml = '<select size="1" name="default_group_id">';
    for(var i=0;i<group_ids.length;i++)
    {
        tmphtml += '<option value="'+group_ids[i]+'"'+(group_ids[i]==<?php echo (int)$member->default_group_id;?>?'selected="selected"':'')+'>'+MyQEE.$('group_name_'+group_ids[i]).innerHTML+'</option>';
    }
    tmphtml += '</select>';
    MyQEE.$('default_perm_div').innerHTML = tmphtml;
}
</script>
<?php
if ($show_edit_perm)
{
?>
<div class="tag">
    <ul class="ul">
        <li id="step_tag_1" onclick="do_per_step()" class="hover">基本信息</li>
        <li id="step_tag_2" onclick="do_next_step()">权限设置</li>
    </ul>
</div>
<?php
}
?>
<form name="myform" id="myform" method="post" onsubmit="return MyQEE.form_post(this,document.referrer);">
<?php echo Form::hidden('edit_perm',$show_edit_perm?'1':'0');?>
<div id="mytag_main_1">
<table border="0" cellpadding="4" cellspacing="1" align="center" class="mainTable">
    <tr>
        <th colspan="2"><?php echo $title;?> - 基本信息</th>
    </tr>
    <?php
    if (!$member->id>0)
    {
        # 创建新用户
    ?>
    <tr>
        <td width="100" class="td1" align="right">
           用户名：
        </td>
        <td class="td2">
            <input type="text" value="" name="username" style="width:120px;" />
        </td>
    </tr>
    <tr>
        <td class="td1" align="right">
            密码：
        </td>
        <td class="td2">
            <input type="password" value="" name="new_password" style="width:120px;" />
        </td>
    </tr>
    <tr>
        <td class="td1" align="right">
            确认密码：
        </td>
        <td class="td2">
            <input type="password" value="" name="new_password_2" style="width:120px;" />
        </td>
    </tr>
    <?php
    }
    else
    {
    ?>
    <tr>
        <td width="100" class="td1" align="right">
           用户名：
        </td>
        <td class="td2">
            <?php echo Form::input('',$member->username,array('style'=>'width:120px;','disabled'=>'disabled'));?>
        </td>
    </tr>
    <?php
    }
    ?>
    <tr>
        <td class="td1" align="right">
           昵称：
        </td>
        <td class="td2">
            <?php echo Form::input('nickname',$member->nickname,array('style'=>'width:120px;'));?>
        </td>
    </tr>
    <?php
    if ( Session::instance()->member()->perm()->is_super_perm() )
    {
    ?>
    <tr>
        <td class="td1" align="right">所属项目：</td>
        <td class="td2">
            <?php
            if( !$member->id )
            {
                foreach ( Core::config('core.projects') as $k=>$v )
                {
                    if ( $v['isuse']===false )
                    {
                        continue;
                    }
                    $p_arr[$k] = $v['name'];
                }
                echo Form::select('project',$p_arr,Core::$project);
                echo ' <font style="color:red">创建后不可修改</font>';
            }
            else
            {
                echo Core::config('core.projects.'.$member->project.'.name');
            }
            ?>
        </td>
    </tr>
    <?php
    }
    ?>
    <?php
    if ( $show_edit_perm )
    {
    ?>
    <tr>
        <td class="td1" align="right">所属权限组：</td>
        <td colspan="2" class="td2">
            <ul class="ul" id="checked_perm_div">
            <?php
            $groupids = ($member->group_ids);
            if (!$groupids)$groupids = array();
            if ( $groups )foreach ($groups as $group)
            {
                if (false)$group = new ORM_Admin_MemberGroup_Data();
                echo '<li style="padding-right:20px;float:left;display:inline;"><label for="group_'.$group->id.'">'.Form::checkbox('group_ids[]',$group->id,in_array($group->id, $groupids),array('id'=>'group_'.$group->id,'onclick'=>'change_group();')).'<span id="group_name_'.$group->id.'">'.$group->group_name.'</span></label></li>';
            }
            ?>
            </ul>
        </td>
    </tr>
    <tr>
        <td width="100" class="td1" align="right">默认权限组：</td>
        <td class="td2" id="default_perm_div">

        </td>
    </tr>
    <?php
    }
    ?>
    <tr>
        <td class="td1"> </td>
        <td class="td1">
        <?php
        if ($show_edit_perm)
        {
        ?>
        <input type="button" class="submit" onclick="do_next_step()" value="下一步" />
        <?php
        }
        else
        {
        ?>
        <input type="button" class="submit" onclick="document.forms['myform'].onsubmit();" value="立即保存" />
        <?php
        }
        ?>
        <input type="button" value="返回" onclick="window.history.go(-1)" />
        <span id="step1_loading" style="display:none;">请稍等...</span>
        </td>
    </tr>
</table>
</div>

<script type="text/javascript">change_group();</script>

<div id="mytag_main_2" style="display:none;">
<table border="0" cellpadding="4" cellspacing="1" align="center" class="mainTable">
    <tr>
        <th colspan="2"><?php echo $title;?> - 权限设置</th>
    </tr>
    <tr>
        <td width="100" class="td1" align="right">权限设置：</td>
        <td class="td2" id="perm_div">
        </td>
    </tr>
    <tr>
        <td class="td1"> </td>
        <td class="td1">
        <input type="button" class="submit" onclick="document.forms['myform'].onsubmit();" value="立即保存" />
        <input type="button" value="上一步" onclick="do_per_step()" />
        <input type="button" value="返回" onclick="window.history.go(-1)" />
        </td>
    </tr>
</table>
</div>
</form>